﻿@using AM
@inject IConfiguration _configuration


<img src="@DataSrc()" class="card-img-top" alt="Обложка"/>

@code
{
    /// <summary>
    /// Номер картинки.
    /// </summary>
    [Parameter]
    public string? Number { get; set; }

    private string? _path;

    private string DataSrc()
    {
        const string NoCover = "img/nocover.png";

        _path ??= _configuration["image-path"];
        if (string.IsNullOrWhiteSpace (_path))
        {
            return NoCover;
        }

        var fileName = Number;
        if (string.IsNullOrWhiteSpace (fileName))
        {
            return NoCover;
        }

        if (!fileName.Contains ('.'))
        {
            fileName += ".jpg";
        }

        fileName = Path.Combine (_path, fileName);
        if (!File.Exists (fileName))
        {
            return NoCover;
        }

        var bytes = File.ReadAllBytes (fileName);
        var base64 = Convert.ToBase64String (bytes);
        var result = "data:image/jpeg;base64," +  base64;

        return result;
    }
}
